export function pidToChildren(data, config, addRoot) {
  var id = config.id || 'id'
  var pid = config.pid || 'pid'
  var children = config.children || 'children'
  var label = config.label || 'label'
  var _addRoot = addRoot || false
  // var label=config.label || 'LctnName';
  var idMap = {}
  var jsonTree = []
  if (_addRoot) {
    const rootMap = {}
    rootMap[id] = '0'
    rootMap[label] = '全部'
    data.push(rootMap)
  }
  data.forEach(function(v) {
    idMap[v[id]] = v
  })
  data.forEach(function(v) {
    var parent = idMap[v[pid]]
    if (parent) {
      !parent[children] && (parent[children] = [])
      parent[children].push(v)
    } else {
      jsonTree.push(v)
    }
  })
  return jsonTree
}

export function showLabelByPid(data, idValue, config = { id: 'id', label: 'label', pid: 'pid' }) {
  const label = []
  const find = (_data, _config, _idValue, _label) => {
    const current = _data.find(k => k[_config['id']] === _idValue)
    if (current) {
      _label.unshift(current[_config['label']])
      if (current.pid !== '0') {
        find(_data, _config, current[_config['pid']], _label)
      }
    }
  }
  find(data, config, idValue, label)
  return label
}

